package com.cloudptt.api.product.func.media.light;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.SystemClock;
import android.preference.PreferenceManager;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.pjsip.api.BaseCallConfig;
import com.cloudptt.api.product.log.Log;

public class LightManager implements IBreathingLight, OnSharedPreferenceChangeListener {

	private static String THIS_FILE = "LightManager";
//	private final static  int Time = 5000;  
	public final static int Level_High = 3;
	public final static int Level_Operate = 2;
	public final static int Level_Low = 1;
	public final static int Level_Own = 0;

	public final static int LED_RED = 0;
	public final static int LED_GREE = 1;
	public final static int LED_BLUE = 2;

	private Context ctx = null;
	private Notification lightNotification = null; 
	private BreathingLightReceiver receiver = null;
	private SharedPreferences sp = null;
	private volatile static int level = Level_Own;
	private int blnTimer = 5;
//	private boolean isLogined = false;

	public LightManager(Context ctx)
	{
//		Log.d(THIS_FILE, "LightManager");
		this.ctx = ctx;
		this.sp = PreferenceManager.getDefaultSharedPreferences(ctx);
		this.sp.registerOnSharedPreferenceChangeListener(this);
		String value = sp.getString(BaseCallConfig.Timer_BLN, "" + BaseCallConfig.Default_Timer_BLN);
		blnTimer = CheckData.parseInt(value);
//		Log.d(THIS_FILE, "LightManager blnTimer = " + blnTimer);
	}
	public void clearResouce()
	{
		Log.d(THIS_FILE, "clearResouce");
		stopBreathingLight();
		lightNotification = null;
		ctx = null;
	}
	public void onLogin(boolean isLogin)
	{
		if(isLogin)
		{
			 if(blnTimer != 0)
			 {
				 startBreathingLight();
			 }
		}
		else
		{
			stopBreathingLight();
		}
	}
	private void startBreathingLight()
	{
//		Log.d(THIS_FILE, "startBreathingLight");
		stopBreathingLight();
		
		receiver = new BreathingLightReceiver(ctx,this);

		Intent intent =new Intent(BreathingLightReceiver.ALARM_TIMER_ACTION);
		PendingIntent sender=PendingIntent.getBroadcast(ctx, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		//瀵拷婵妞傞梻锟�
		long firstime=SystemClock.elapsedRealtime();

		AlarmManager am=(AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
		//娑擄拷娑擃亜鎳嗛張鐕傜礉娑撳秴浠犻惃鍕絺闁礁绠嶉幘锟�
		am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime,blnTimer * 1000, sender);
	}
	
	private void stopBreathingLight()
	{
//		Log.d(THIS_FILE, "stopBreathingLigh");
		if(receiver != null)
		{
			receiver.clearResource();
			receiver = null;
		}
		Intent intent =new Intent(BreathingLightReceiver.ALARM_TIMER_ACTION);
		PendingIntent sender=PendingIntent.getBroadcast(ctx, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
		AlarmManager alarm=(AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
		alarm.cancel(sender);
	}

	final int ID_LED=20160526; 
	public void closeLED(int lev) 
	{
//		Log.d(THIS_FILE, "closeLED level = " + level + " lev = " + lev);
//		if(!isLogined)
//		{
//			return;
//		}
		if(level != lev)
		{
			return;
		}
//		Log.d(THIS_FILE, "closeLED ");
		level = Level_Own;
		
		NotificationManager nm = (NotificationManager) ctx.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
		nm.cancelAll();//.cancel(ID_LED);
	}
	
	public void resetLED() 
	{
		Log.d(THIS_FILE, "resetLED");
		closeLED(Level_High);
	}

	public void openLED(boolean isFlash, int ledColor,int lev) 
	{
//		Log.d(THIS_FILE, "openLED isFlash = " + isFlash + " ledColor = " + ledColor + " level = " + level + " lev = " + lev);
		if(level >= lev)
		{
			return;
		}
		level = lev;
		
		Log.d(THIS_FILE, "openLED");
		
		NotificationManager nm = (NotificationManager) ctx.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

		if (lightNotification == null) {
			lightNotification = new Notification();
		}

		if(ledColor==0){
			lightNotification.ledARGB = 0xffFF0000; 
		}else if(ledColor==1){
			lightNotification.ledARGB = 0xff00FF00; 
		}else if(ledColor==2){
			lightNotification.ledARGB = 0xff0000FF; 
		}else {
			return;
		}

		if(!isFlash)
		{
			lightNotification.ledOffMS = 0; 
		}
		else
		{
			lightNotification.ledOffMS = 1000; 
		}
		lightNotification.flags = Notification.FLAG_SHOW_LIGHTS;
		nm.notify(ID_LED, lightNotification);
	}
	
	public void openBreathingLight() 
	{
//		Log.d(THIS_FILE, "openBreathingLight level = " + level);
		if(level != Level_Own)
		{
			return;
		}
		
//		Log.d(THIS_FILE, "openBreathingLight");
		NotificationManager nm = (NotificationManager) ctx.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

		if (lightNotification == null) {
			lightNotification = new Notification();
		}

		lightNotification.ledARGB = 0xff00FF00; 
		lightNotification.ledOnMS = 500;  
		lightNotification.ledOffMS = 5000; 
		lightNotification.flags = Notification.FLAG_SHOW_LIGHTS;
		nm.notify(ID_LED, lightNotification);
	}
	
	public void closeBreathingLight() 
	{
		if(level != Level_Own)
		{
			return;
		}
//		Log.d(THIS_FILE, "closeBreathingLight");
		NotificationManager nm = (NotificationManager) ctx.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
		nm.cancelAll();
	}
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		// TODO Auto-generated method stub
		if(key.equals(BaseCallConfig.Timer_BLN))
		{
			String value = sp.getString(BaseCallConfig.Timer_BLN, "" + BaseCallConfig.Default_Timer_BLN);
			int iValue = CheckData.parseInt(value);
//			Log.d(THIS_FILE, "onSharedPreferenceChanged iValue = " + iValue + " blnTimer = " + blnTimer);
			if(iValue != blnTimer)
			{
				blnTimer = iValue;
				this.stopBreathingLight();
				if(iValue != 0)
				{
					this.startBreathingLight();
				}
			}
		}
	}
}
